package vip.xiaonuo.supplier.modular.finance.accounttype.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.finance.accounttype.entity.SupplierAccountType;
import vip.xiaonuo.supplier.modular.finance.accounttype.mapper.SupplierAccountTypeMapper;
import vip.xiaonuo.supplier.modular.finance.accounttype.param.SupplierAccountTypeAddParam;
import vip.xiaonuo.supplier.modular.finance.accounttype.param.SupplierAccountTypeEditParam;
import vip.xiaonuo.supplier.modular.finance.accounttype.param.SupplierAccountTypeIdParam;
import vip.xiaonuo.supplier.modular.finance.accounttype.param.SupplierAccountTypePageParam;
import vip.xiaonuo.supplier.modular.finance.accounttype.service.SupplierAccountTypeService;

import java.util.List;

/**
 * 账户类型Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/17 17:12
 **/
@Service
public class SupplierAccountTypeServiceImpl extends ServiceImpl<SupplierAccountTypeMapper, SupplierAccountType> implements SupplierAccountTypeService {

    @Override
    public Page<SupplierAccountType> page(SupplierAccountTypePageParam supplierAccountTypePageParam) {
        QueryWrapper<SupplierAccountType> queryWrapper = new QueryWrapper<SupplierAccountType>().checkSqlInjection();
        //数据归属
        queryWrapper.lambda().eq(SupplierAccountType::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierAccountTypePageParam.getSortField(), supplierAccountTypePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierAccountTypePageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierAccountTypePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierAccountTypePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierAccountType::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierAccountTypeAddParam supplierAccountTypeAddParam) {
        SupplierAccountType supplierAccountType = BeanUtil.toBean(supplierAccountTypeAddParam, SupplierAccountType.class);
        //数据归属
        supplierAccountType.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierAccountType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierAccountTypeEditParam supplierAccountTypeEditParam) {
        SupplierAccountType supplierAccountType = this.queryEntity(supplierAccountTypeEditParam.getId());
        BeanUtil.copyProperties(supplierAccountTypeEditParam, supplierAccountType);
        this.updateById(supplierAccountType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierAccountTypeIdParam> supplierAccountTypeIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierAccountTypeIdParamList, SupplierAccountTypeIdParam::getId));
    }

    @Override
    public SupplierAccountType detail(SupplierAccountTypeIdParam supplierAccountTypeIdParam) {
        return this.queryEntity(supplierAccountTypeIdParam.getId());
    }

    @Override
    public SupplierAccountType queryEntity(String id) {
        SupplierAccountType supplierAccountType = this.getById(id);
        if(ObjectUtil.isEmpty(supplierAccountType)) {
            throw new CommonException("账户类型不存在，id值为：{}", id);
        }
        return supplierAccountType;
    }
}
